AddCSLuaFile( "cl_init.lua" ) -- Make sure clientside  
AddCSLuaFile( "shared.lua" )  --and shared scripts are sent.     
include('shared.lua')     

local maxrocks = 100
local currocks = 0

function ENT:SpawnFunction( ply, tr )
	if ( not tr.Hit ) then return end
	local SpawnPos = tr.HitPos
	local ent = ents.Create("sent_spaceanon_asteroid_field")
	ent:SetPos(SpawnPos)
	ent:Spawn()
	ent:Activate()
	ent:Start()
	return ent
end

function ENT:Initialize()   
	self.Entity:SetModel("models/props_junk/watermelon01.mdl")  	
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
end

function ENT:Start()
	local rocks = math.random(15,30)
	local i = 0
	for i=1, rocks do
		if currocks >= maxrocks then return end
		local pos = self.Entity:GetPos()+Vector(math.random(-768,768),math.random(-768,768),math.random(-768,768))
		rock = ents.Create("sent_spaceanon_asteroid")
		rock:SetPos(pos)
		rock:SetAngles(Angle(math.random(0,360),math.random(0,360),math.random(0,360)))
		rock:Spawn()
		while true do
			if rock:IsInWorld() then
				rock:GetPhysicsObject():EnableGravity(false)
				rock:GetPhysicsObject():SetMass(2500)
				currocks = currocks+1
				break
			end
			pos = self.Entity:GetPos()+Vector(math.random(-768,768),math.random(-768,768),math.random(-768,768))
			rock:SetPos(pos)
		end
	end
	self.Entity:Remove()
end